/**
 * 
 */
package com.dalonedrow.rpg.base.flyweights;

import com.dalonedrow.globalvariables.constants.hq.GlobalConstants;

/**
 * @author Donald
 */
public final class ScriptVariable {
	/** the floating-point array value the {@link ScriptVariable} references. */
	private float[]	faval;
	/** the floating-point value the {@link ScriptVariable} references. */
	private float	fval;
	/** the integer array value the {@link ScriptVariable} references. */
	private int[]	iaval;
	/** the integer value the {@link ScriptVariable} references. */
	private int		ival;
	/** the long array value the {@link ScriptVariable} references. */
	private long[]	laval;
	/** the long value the {@link ScriptVariable} references. */
	private long	lval;
	/** the {@link ScriptVariable}'s name. */
	private byte[]	name;
	/** the string value the {@link ScriptVariable} references. */
	private byte[]	text;
	/** the {@link ScriptVariable}'s type. */
	private int		type;
	/**
	 * Creates a new instance of {@link ScriptVariable}.
	 * @param newName the variable name
	 * @param newType the variable type
	 * @param value the variable value
	 */
	public ScriptVariable(final String newName, final int newType,
			final Object value) {
		super();
		name = newName.getBytes();
		type = newType;
		switch (type) {
		case GlobalConstants.TYPE_G_00_TEXT:
		case GlobalConstants.TYPE_L_07_TEXT:
			text = ((String) value).getBytes();
			break;
		case GlobalConstants.TYPE_G_01_FLOAT:
		case GlobalConstants.TYPE_L_08_FLOAT:
			fval = ((Float) value).floatValue();
			break;
		case GlobalConstants.TYPE_G_02_FLOAT_ARR:
		case GlobalConstants.TYPE_L_09_FLOAT_ARR:
			faval = (float[]) value;
			break;
		case GlobalConstants.TYPE_G_03_INT:
		case GlobalConstants.TYPE_L_10_INT:
			ival = ((Integer) value).intValue();
			break;
		case GlobalConstants.TYPE_G_04_INT_ARR:
		case GlobalConstants.TYPE_L_11_INT_ARR:
			iaval = (int[]) value;
			break;
		case GlobalConstants.TYPE_G_05_LONG:
		case GlobalConstants.TYPE_L_12_LONG:
			lval = ((Long) value).longValue();
			break;
		case GlobalConstants.TYPE_G_06_LONG_ARR:
		case GlobalConstants.TYPE_L_13_LONG_ARR:
			laval = (long[]) value;
			break;
		default:
			break;
		}
	}
	/** Clears up member fields, releasing their memory. */
	public void clear() {
		name = null;
		text = null;
		iaval = null;
		laval = null;
	}
	/**
	 * Gets the floating-point array value the {@link ScriptVariable}
	 * references.
	 * @return <code>float[]</code>
	 */
	public float[] getFloatArrayVal() {
		return faval;
	}
	/**
	 * Gets a floating-point value from the floating-point array the
	 * {@link ScriptVariable} references.
	 * @param index the value's index
	 * @return <code>float</code>
	 */
	public float getFloatArrayVal(final int index) {
		return faval[index];
	}
	/**
	 * Gets the floating-point value the {@link ScriptVariable} references.
	 * @return <code>float</code>
	 */
	public float getFloatVal() {
		return fval;
	}
	/**
	 * Gets the integer array value the {@link ScriptVariable} references.
	 * @return <code>int[]</code>
	 */
	public int[] getIntArrayVal() {
		return iaval;
	}
	/**
	 * Gets an integer value from the integer array the {@link ScriptVariable}
	 * references.
	 * @param index the value's index
	 * @return <code>int</code>
	 */
	public int getIntArrayVal(final int index) {
		return iaval[index];
	}
	/**
	 * Gets the integer value the {@link ScriptVariable} references.
	 * @return <code>long</code>
	 */
	public int getIntVal() {
		return ival;
	}
	/**
	 * Gets the long integer array value the {@link ScriptVariable} references.
	 * @return <code>long[]</code>
	 */
	public long[] getLongArrayVal() {
		return laval;
	}
	/**
	 * Gets a long integer value from the long integer array the
	 * {@link ScriptVariable} references.
	 * @param index the value's index
	 * @return <code>long</code>
	 */
	public long getLongArrayVal(final int index) {
		return laval[index];
	}
	/**
	 * Gets the long integer value the {@link ScriptVariable} references.
	 * @return <code>long</code>
	 */
	public long getLongVal() {
		return lval;
	}
	/**
	 * Gets the {@link ScriptVariable} 's name.
	 * @return {@link String}
	 */
	public String getName() {
		return new String(name);
	}
	/**
	 * Gets the text value the {@link ScriptVariable} references.
	 * @return {@link String}
	 */
	public String getText() {
		return new String(text);
	}
	/**
	 * Gets the {@link ScriptVariable}'s type.
	 * @return <code>int</code>
	 */
	public int getType() {
		return type;
	}
	/**
	 * Determines if the script variables have a text component.
	 * @return true if the script variables have a text component; false
	 *         otherwise
	 */
	public boolean hasText() {
		boolean has = false;
		if (text != null && text.length > 0) {
			has = true;
		}
		return has;
	}
	/**
	 * Adds a value to the floating-point array the {@link ScriptVariable}
	 * references.
	 * @param val the floating-point value to add
	 */
	public void setFloatArrayVal(final float val) {
		if (faval == null) {
			faval = new float[0];
		}
		// add a new value
		float[] dest = new float[faval.length + 1];
		System.arraycopy(faval, 0, dest, 0, faval.length);
		dest[faval.length] = val;
		faval = dest;
		dest = null;
	}
	/**
	 * Sets the floating-point array value the {@link ScriptVariable}
	 * references.
	 * @param val the floating-point array value to set
	 */
	public void setFloatArrayVal(final float[] val) {
		faval = val;
	}
	/**
	 * Adds a value to the floating-point array the {@link ScriptVariable}
	 * references at a specific index.
	 * @param index the index where the value is being added
	 * @param val the floating-point value to add
	 */
	public void setFloatArrayVal(final int index, final float val) {
		if (faval == null) {
			faval = new float[0];
		}
		if (index >= iaval.length) {
			// add a new value
			float[] dest = new float[index + 1];
			System.arraycopy(faval, 0, dest, 0, faval.length);
			faval = dest;
			dest = null;
		}
		faval[index] = val;
	}
	/**
	 * Sets the floating-point value the {@link ScriptVariable} references.
	 * @param val the floating-point value to set
	 */
	public void setFloatVal(final float val) {
		fval = val;
	}
	/**
	 * Adds a value to the integer array the {@link ScriptVariable} references.
	 * @param val the integer value to add
	 */
	public void setIntArrayVal(final int val) {
		if (iaval == null) {
			iaval = new int[0];
		}
		// add a new value
		int[] dest = new int[iaval.length + 1];
		System.arraycopy(iaval, 0, dest, 0, iaval.length);
		dest[iaval.length] = val;
		iaval = dest;
		dest = null;
	}
	/**
	 * Adds a value to the integer array the {@link ScriptVariable} references
	 * at a specific index.
	 * @param index the index where the value is being added
	 * @param val the integer value to add
	 */
	public void setIntArrayVal(final int index, final int val) {
		if (iaval == null) {
			iaval = new int[0];
		}
		if (index >= iaval.length) {
			// add a new value
			int[] dest = new int[index + 1];
			System.arraycopy(iaval, 0, dest, 0, iaval.length);
			iaval = dest;
			dest = null;
		}
		iaval[index] = val;
	}
	/**
	 * Sets the integer array value the {@link ScriptVariable} references.
	 * @param val the integer array value to set
	 */
	public void setIntArrayVal(final int[] val) {
		iaval = val;
	}
	/**
	 * Sets the integer value the {@link ScriptVariable} references.
	 * @param val the integer value to set
	 */
	public void setIntVal(final int val) {
		ival = val;
	}
	/**
	 * Adds a value to the long integer array the {@link ScriptVariable}
	 * references at a specific index.
	 * @param index the index where the value is being added
	 * @param val the long integer value to add
	 */
	public void setLongArrayVal(final int index, final long val) {
		if (laval == null) {
			laval = new long[0];
		}
		if (index >= laval.length) {
			// add a new value
			long[] dest = new long[index + 1];
			System.arraycopy(laval, 0, dest, 0, laval.length);
			laval = dest;
			dest = null;
		}
		laval[index] = val;
	}
	/**
	 * Adds a value to the long integer array the {@link ScriptVariable}
	 * references.
	 * @param val the long integer value to add
	 */
	public void setLongArrayVal(final long val) {
		if (laval == null) {
			laval = new long[0];
		}
		// add a new value
		long[] dest = new long[laval.length + 1];
		System.arraycopy(laval, 0, dest, 0, laval.length);
		dest[laval.length] = val;
		laval = dest;
		dest = null;
	}
	/**
	 * Sets the long integer array value the {@link ScriptVariable} references.
	 * @param val the long integer array value to set
	 */
	public void setLongArrayVal(final long[] val) {
		laval = val;
	}
	/**
	 * Sets the long integer value the {@link ScriptVariable} references.
	 * @param val the long integer value to set
	 */
	public void setLongVal(final long val) {
		lval = val;
	}
	/**
	 * Sets the {@link ScriptVariable}'s name.
	 * @param val the name to set
	 */
	public void setName(final String val) {
		name = val.getBytes();
	}
	/**
	 * Sets the text component for the script variable.
	 * @param val the text to be set
	 */
	public void setText(final String val) {
		text = val.getBytes();
	}
	/**
	 * Sets the {@link ScriptVariable}'s type.
	 * @param val the type to set
	 */
	public void setType(final int val) {
		type = val;
	}
}
